﻿using System;
using Terraria.ID;
using Terraria;
using Terraria.ModLoader;
using Microsoft.Xna.Framework;
using EAS.Common.Players;

namespace EAS.Content.Projectiles
{
    public class DevilMinion : ModProjectile
    {
        public override void SetStaticDefaults()
        {
            Main.projFrames[Projectile.type] = 5;
            ProjectileID.Sets.MinionSacrificable[Projectile.type] = true;
            ProjectileID.Sets.CultistIsResistantTo[Projectile.type] = true;
            ProjectileID.Sets.MinionTargettingFeature[Projectile.type] = true;
        }


        public override void SetDefaults()
        {
            Projectile.netImportant = true;
            Projectile.width = 22;
            Projectile.height = 50;
            Projectile.friendly = true;
            Main.projPet[Projectile.type] = true;
            Projectile.minion = true;
            Projectile.minionSlots = 1f;
            Projectile.penetrate = -1;
            Projectile.timeLeft = 18000;
            Projectile.tileCollide = false;
            Projectile.ignoreWater = true;
        }
        public override void AI()
        {
            Projectile.frameCounter++;
            if (Projectile.frameCounter >= 8)
            {
                Projectile.frameCounter = 0;
                Projectile.frame++;
            }
            if (Projectile.frame > 4)
            {
                Projectile.frame = 0;
            }
            Player player = Main.player[Projectile.owner];
            if (player.dead)
            {
                AccPlayer.DevilMinion = false;
            }
            if (AccPlayer.DevilMinion)
            {
                Projectile.timeLeft = 2;
            }
            dust--;
            if (dust >= 0)
            {
                int num501 = 50;
                for (int num502 = 0; num502 < num501; num502++)
                {
                    int num503 = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y + 16f), Projectile.width, Projectile.height - 16, DustID.Torch, 0f, 0f, 0, default, 1f);
                    Main.dust[num503].velocity *= 2f;
                    Main.dust[num503].scale *= 1.15f;
                }
            }
            Projectile.frameCounter++;
            if (Projectile.frameCounter > 8)
            {
                Projectile.frame++;
                Projectile.frameCounter = 0;
            }
            if (Projectile.frame > 4)
            {
                Projectile.frame = 0;
            }
            float num504 = Main.rand.Next(90, 111) * 0.01f;
            num504 *= Main.essScale;
            Lighting.AddLight(Projectile.Center, 1f * num504, 0f * num504, 0.15f * num504);
            Projectile.rotation = Projectile.velocity.X * 0.04f;
            if ((double)Math.Abs(Projectile.velocity.X) > 0.2)
            {
                Projectile.spriteDirection = -Projectile.direction;
            }
            float num505 = 700f;
            float num506 = 800f;
            float num507 = 1200f;
            float num508 = 150f;
            float num509 = 0.05f;
            for (int num510 = 0; num510 < 1000; num510++)
            {
                bool flag23 = Main.projectile[num510].type == Mod.Find<ModProjectile>("DevilMinion").Type;
                if (num510 != Projectile.whoAmI && Main.projectile[num510].active && Main.projectile[num510].owner == Projectile.owner && flag23 && Math.Abs(Projectile.position.X - Main.projectile[num510].position.X) + Math.Abs(Projectile.position.Y - Main.projectile[num510].position.Y) < Projectile.width)
                {
                    if (Projectile.position.X < Main.projectile[num510].position.X)
                    {
                        Projectile.velocity.X = Projectile.velocity.X - num509;
                    }
                    else
                    {
                        Projectile.velocity.X = Projectile.velocity.X + num509;
                    }
                    if (Projectile.position.Y < Main.projectile[num510].position.Y)
                    {
                        Projectile.velocity.Y = Projectile.velocity.Y - num509;
                    }
                    else
                    {
                        Projectile.velocity.Y = Projectile.velocity.Y + num509;
                    }
                }
            }
            bool flag24 = false;
            if (Projectile.ai[0] == 2f)
            {
                Projectile.ai[1] += 1f;
                Projectile.extraUpdates = 1;
                if (Projectile.ai[1] > 40f)
                {
                    Projectile.ai[1] = 1f;
                    Projectile.ai[0] = 0f;
                    Projectile.extraUpdates = 0;
                    Projectile.numUpdates = 0;
                    Projectile.netUpdate = true;
                }
                else
                {
                    flag24 = true;
                }
            }
            if (flag24)
            {
                return;
            }
            Vector2 vector46 = Projectile.position;
            bool flag25 = false;
            if (Projectile.ai[0] != 1f)
            {
                Projectile.tileCollide = false;
            }
            if (Projectile.tileCollide && WorldGen.SolidTile(Framing.GetTileSafely((int)Projectile.Center.X / 16, (int)Projectile.Center.Y / 16)))
            {
                Projectile.tileCollide = false;
            }
            if (player.HasMinionAttackTargetNPC)
            {
                NPC nPC2 = Main.npc[player.MinionAttackTargetNPC];
                if (nPC2.CanBeChasedBy(Projectile, false))
                {
                    float num511 = Vector2.Distance(nPC2.Center, Projectile.Center);
                    if ((Vector2.Distance(Projectile.Center, vector46) > num511 && num511 < num505 || !flag25) && Collision.CanHitLine(Projectile.position, Projectile.width, Projectile.height, nPC2.position, nPC2.width, nPC2.height))
                    {
                        vector46 = nPC2.Center;
                        flag25 = true;
                    }
                }
            }
            else
            {
                for (int num512 = 0; num512 < 200; num512++)
                {
                    NPC nPC3 = Main.npc[num512];
                    if (nPC3.CanBeChasedBy(Projectile, false))
                    {
                        float num513 = Vector2.Distance(nPC3.Center, Projectile.Center);
                        if ((Vector2.Distance(Projectile.Center, vector46) > num513 && num513 < num505 || !flag25) && Collision.CanHitLine(Projectile.position, Projectile.width, Projectile.height, nPC3.position, nPC3.width, nPC3.height))
                        {
                            num505 = num513;
                            vector46 = nPC3.Center;
                            flag25 = true;
                        }
                    }
                }
            }
            float num514 = num506;
            if (flag25)
            {
                num514 = num507;
            }
            if (Vector2.Distance(player.Center, Projectile.Center) > num514)
            {
                Projectile.ai[0] = 1f;
                Projectile.tileCollide = false;
                Projectile.netUpdate = true;
            }
            if (flag25 && Projectile.ai[0] == 0f)
            {
                Vector2 vector47 = vector46 - Projectile.Center;
                float num519 = vector47.Length();
                vector47.Normalize();
                if (num519 > 200f)
                {
                    float scaleFactor2 = 8f;
                    vector47 *= scaleFactor2;
                    Projectile.velocity = (Projectile.velocity * 40f + vector47) / 41f;
                }
                else
                {
                    float num515 = 4f;
                    vector47 *= -num515;
                    Projectile.velocity = (Projectile.velocity * 40f + vector47) / 41f;
                }
            }
            else
            {
                bool flag26 = false;
                if (!flag26)
                {
                    flag26 = Projectile.ai[0] == 1f;
                }
                float num516 = 5f;
                if (flag26)
                {
                    num516 = 12f;
                }
                Vector2 center2 = Projectile.Center;
                Vector2 vector48 = player.Center - center2 + new Vector2(0f, -30f);
                float num520 = vector48.Length();
                if (num520 > 200f && num516 < 6.5f)
                {
                    num516 = 6.5f;
                }
                if (num520 < num508 && flag26 && !Collision.SolidCollision(Projectile.position, Projectile.width, Projectile.height))
                {
                    Projectile.ai[0] = 0f;
                    Projectile.netUpdate = true;
                }
                if (num520 > 2000f)
                {
                    Projectile.position.X = Main.player[Projectile.owner].Center.X - Projectile.width / 2;
                    Projectile.position.Y = Main.player[Projectile.owner].Center.Y - Projectile.height / 2;
                    Projectile.netUpdate = true;
                }
                if (num520 > 70f)
                {
                    vector48.Normalize();
                    vector48 *= num516;
                    Projectile.velocity = (Projectile.velocity * 40f + vector48) / 41f;
                }
                else if (Projectile.velocity.X == 0f && Projectile.velocity.Y == 0f)
                {
                    Projectile.velocity.X = -0.2f;
                    Projectile.velocity.Y = -0.1f;
                }
            }
            if (Projectile.ai[1] > 0f)
            {
                Projectile.ai[1] += Main.rand.Next(1, 4);
            }
            if (Projectile.ai[1] > 80f)
            {
                Projectile.ai[1] = 0f;
                Projectile.netUpdate = true;
            }
            if (Projectile.ai[0] == 0f)
            {
                float scaleFactor3 = 11f;
                int num517 = ModContent.ProjectileType<DevilProj>();
                if (flag25 && Projectile.ai[1] == 0f)
                {
                    Projectile.ai[1] += 1f;
                    if (Main.myPlayer == Projectile.owner && Collision.CanHitLine(Projectile.position, Projectile.width, Projectile.height, vector46, 0, 0))
                    {
                        Vector2 value19 = vector46 - Projectile.Center;
                        value19.Normalize();
                        value19 *= scaleFactor3;
                        int num518 = Projectile.NewProjectile(null, Projectile.Center.X, Projectile.Center.Y, value19.X, value19.Y, num517, Projectile.damage, 0f, Main.myPlayer, 0f, 0f);
                        Main.projectile[num518].timeLeft = 300;
                        Projectile.netUpdate = true;
                    }
                }
            }
        }


        private int dust = 3;
    }
}
